import ajax from '../util/ajax.js'
var html = document.documentElement;
const layout = html.clientWidth || document.body.clientWidth;
html.style.fontSize = layout / 3.75 + 'px';
ajax('http://39.106.114.31:8080/list').then((data) => {
  $.each(data, function (key, value) {
    $('#goodUl').append(`<li cid=${value.cid} class=goodLi>${value.name}</li>`);
  })
  go()
})
let cid = 10001
superajax(10001)
$('#goodUl').delegate('li', 'click', (e) => {
  cid = $(e.target).attr('cid')
  $('.goodDetail').empty()
  $('.detailUl').empty()
  superajax(cid)
  $('#goodUl').children().removeClass('currentLi')
  $(e.target).addClass('currentLi')
})

function go() {
  $('.goodLi:first-child').addClass('currentLi')
}

function superajax(cid) {
  ajax('http://39.106.114.31:8080/data', cid).then((data) => {
    if (data.bannerImg) {
      $('#goodDetail').append(`
    <div class=bannerImg>
    <img src=${data.bannerImg}>
    </div>
    `)
    }
    $.each(data.content, function (key, value) {
      $('#goodDetail').append(`
      <div class=sortTitle>
      <p>${value.title}</p>
      </div>
      `)
      $.each(value.list, function (key1, value1) {
        $('#goodDetail').append(`
          <a href=./good.html?id=${key1}><figure class=sortFigure>
          <img src=${value1.imgSrc}>
          <p>${value1.text}</p>
         </figure></a>
        `)
      })
    })
  })
}